package cn.szzsi._web;


import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.RegexUtils;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.zshop.CompanyInfo;
import cn.szzsi.common.sms.SmsEmayUtil;
import cn.szzsi.common.sms.SmsZhuanxinUtil;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 前台类 - 密码、安全问题
 *
 */
public class PasswordController extends BaseWebController<Member> {

	private Member member;
	private String oldPassword;
	private String path;//模板路径
	private String shopId;//用户的shopId就是商户的id
	
	// 密码修改
	public void edit() {
		shopId=getLoginMember().getStr("shopId");
		setAttr("shopid",shopId);
		setAttr("member", getLoginMember());
		getTemplatePath(shopId,"password_input");
	}
	
	// 密码更新
	public void update() {
		member = getModel(Member.class);
		oldPassword = getPara("oldPassword","");
		Member persistent = getLoginMember();
		String shopid = getPara("shopid");
		String path= CommonUtil.getPathByUidAndType(shopid, CommonUtil.isMobileDevice(getRequest()));//获取模板路径
		if (StrKit.notBlank(oldPassword) && StrKit.notBlank(member.getStr("password"))) {
			String oldPasswordMd5 = DigestUtils.md5Hex(oldPassword);
			if (!StringUtils.equals(persistent.getStr("password"), oldPasswordMd5)) {
				renderHtml("<script>alert('旧密码不正确!');history.go(-1);</script>");
				return;
			}
			String newPasswordMd5 = DigestUtils.md5Hex(member.getStr("password"));
			persistent.set("password",newPasswordMd5);
		}
		if (StrKit.notBlank(member.getStr("safeQuestion")) && StrKit.notBlank(member.getStr("safeAnswer"))) {
			persistent.set("safeQuestion",member.getStr("safeQuestion"));
			persistent.set("safeAnswer",member.getStr("safeAnswer"));
		}
		
		updated(persistent);
		renderWapSuccessMessage("更新成功!","/b/password/edit?shopId="+shopid,path);
	}


	/**
	 * 密码找回
	 */
	@Clear
	public void forgetPassword(){
		wapTemplatePath("forget_password.html");
	}

	/**
	 *
	 */
	@Clear
	public void updatePasswordByPhone(){
		String phone =getPara("phone");
		String phoneCaptcha=getPara("phoneCaptcha");
		String password=getPara("password");
		if(StrKit.isBlank(phone)){
			renderJson(Ret.fail("info","请输入正确的手机号!"));
			return;
		}
		/*if(StrKit.notBlank(phone)&&!RegexUtils.checkMobile(phone)){
			renderJson(Ret.fail("info","请输入正确的手机号!"));
			return;
		}*/

		if(CacheKit.get("sms",phone)==null){
			renderJson(Ret.fail("info","手机验证码已过期,请重新获取!"));
			return;
		}
		if(!phoneCaptcha.equals(CacheKit.get("sms",phone).toString())){
			renderJson(Ret.fail("info","手机验证码不正确!"));
			return;
		}

		Member member= Member.dao.getByPhone(phone);
		if(member==null){
			renderJson(Ret.fail("info","该手机号没有注册!"));
			return;
		}
		if(!RegexUtils.checkNumLN(password)){
			renderJson(Ret.fail("info","密码只能输入数字、字母或者下划线"));
			return;
		}
		member.setPassword(DigestUtils.md5Hex(password));
		member.update();
		renderJson(Ret.ok());

	}

	/**
	 * 获取手机验证码
	 */
	@Clear
	public void getCaptcha(){
		String phone =getPara("phone");
		if(StrKit.isBlank(phone)){
			renderJson(Ret.fail("info","请输入正确的手机号"));
			return;
		}
		/*if(StrKit.notBlank(phone)&&!RegexUtils.checkMobile(phone)){
			renderJson(Ret.fail("info","请输入正确的手机号"));
			return;
		}*/
		Member member= Member.dao.getByPhone(phone);
		if(member==null){
			renderJson(Ret.fail("info","该手机号没有注册!"));
			return;
		}
		String captcha = CommonUtil.getSixRandom();//六位纯数字验证码
		//同一个手机号限制一分钟之内只能发送一条
		Long lastTimeMillis = CacheKit.get("sms_last_time",phone);
		if(lastTimeMillis!=null){
			Long currentTimeMillis=System.currentTimeMillis();
			if((currentTimeMillis-lastTimeMillis)<60000){
				renderJson(Ret.fail("info","您的操作过于频繁,请稍后再试!!"));
				return;
			}

		}
		//同一个手机号限制一分钟之内只能发送一条
		//【微米荟商城】您的验证码:686260 ，请勿泄露！感谢您的使用！
		String message = "【微米荟商城】您的验证码:"+captcha+" ，请勿泄露！感谢您的使用！";
		boolean sendResult= SmsEmayUtil.sendToSms("0",phone,message,"yzm","wmh",captcha);
		if(sendResult){
			renderJson(Ret.ok("info","发送成功!"));
		}else{
			renderJson(Ret.fail("info","发送失败!"));
		}
	}

}
